<?php
/* Webappkit trigger manager, INI file storage
file version 2005-06-02 00:10:37
*/

class wakTriggerManager extends wakCoreComponent {

/*------------------------------------------------------------------------------
																																		constructor
------------------------------------------------------------------------------*/
	// see parent constructor
	function wakTriggerManager(&$controller) {
		// call of parent constructor
		$const=get_parent_class($this);
		parent::$const($controller);
		// reading triggers storage files
		$file=$this->cfg['path'].$this->cfg['file'];
		if (!$this->readIniFile($file)) {
			// fatal error
			
		}
		// echo wakaffvar($this->triggers);
	}
/*------------------------------------------------------------------------------
																																	public methods
------------------------------------------------------------------------------*/
	/*
	 returns an array of trigger objects
	*/
	function getStateEvents() {
		$events=array();
		foreach ($this->triggers as $id => $trigger) {
			// echo affvar($trigger);
			//if ($trigger['apply_self']!='true') continue;
			//echo affvar($trigger);
			if (strtolower($trigger['method'])!='both' and strtolower($trigger['method'])!=strtolower(REQUEST_METHOD)) {
				//trigger_error("event $id not selected");
				continue; // wrong method call
			}
			if (ereg('^'.$trigger['uri_path'].'/?$',CALL_URI) and $trigger['apply_self']) {
				// apply-self
				$events[]=$trigger;
				// echo wakaffvar($trigger);
			} else if (ereg('^'.$trigger['uri_path'].'(/[^/]+)+',CALL_URI) and $trigger['apply_children']) {
				// parent trigger applying to children
				$events[]=$trigger;
			} else if ($trigger['uri_path']=='/' and $trigger['apply_children']) {
				// home applying to children
				$events[]=$trigger;
			} else {
				// event not selected
				//trigger_error("event $id not selected");
				continue;
			}
		}
		//echo affvar($events);
		// echo "<pre>"; var_dump($events); echo "</pre>";
		return $events;
	}

	// recherche tous les evenements a activer
	function getEvents($callUri=NULL) {
	
		/*$this->C->needLibrary('readcsvfile');
		$data=readcsvfile($this->param['path'].$this->param['file'],';','"');*/
		$file=$this->parameters['path'].$this->parameters['file'];
		
		if (!$lines=file($file)) {
			trigger_error("waktriggerManager: couldn't read events file $file",E_USER_ERROR);
			return false;
		}
		wakLog("triggerManager: events file $file read, ".count($lines)." lines");
		//echo affvar($lines,false);
		$events=array();
		for ($f=0; $f<count($lines); $f++) {
			if (!$data=$this->readCsvLine($lines[$f])) continue;
			// echo affvar($data);
			//if ($data['apply_self']!='true') continue;
			//echo affvar($data);
			if (strtolower($data['method'])!='both' and strtolower($data['method'])!=strtolower(REQUEST_METHOD))
				continue; // wrong method call
			
			if (ereg('^'.$data['uri_path'].'/?$',CALL_URI) and $data['apply_self']=='true') {
				// apply-self
				$events[]=$data;
				// echo wakaffvar($data);
			} else if (ereg('^'.$data['uri_path'].'(/[^/]+)+',CALL_URI) and $data['apply_children']=='true') {
				// parent trigger applying to children
				$events[]=$data;
			} else if ($data['uri_path']=='/' and $data['apply_children']=='true') {
				// home applying to children
				$events[]=$data;
			} else continue; // evt non selectionné
		}
		//echo affvar($events);
		return $events;
	}

/*------------------------------------------------------------------------------
																																			properties
------------------------------------------------------------------------------*/
	var $default_parameters=array(
		'path'=>'cfg/',
		'file'=>'webappkit-triggers.ini'
	);
	
	var $triggers=array();
	

/*------------------------------------------------------------------------------
																																private methods
------------------------------------------------------------------------------*/
// methodes privees

	function readIniFile($file) {
		if (!file_exists($file)) {
			trigger_error("File $file don't exist");
			return false;
		}
		$triggers=parse_ini_file($file,true);
		if (!$triggers) {
			trigger_error("File $file is not an INI file");
			return false;
		}
		$this->triggers=&$triggers;
	}

}
/*------------------------------------------------------------------------------

------------------------------------------------------------------------------*/
?>
